import axios from 'axios'
import { IGoodsProps } from 'src/views/goods/Goods'
import { Config } from '../config/config'

// 获得列表
export async function getGoodsList() {
    return new Promise((resolve, reject) => {
        axios.get(`${Config.API_BASE_URL}/goods/allGoodsInfo`).then((res: any) => {
            resolve(res.data)
        })
    })
}

// 新增
export async function addOneGoods(params: IGoodsProps) {
    return new Promise((resolve, reject) => {
        axios.post(`${Config.API_BASE_URL}/goods/addOneGoods`, { ...params }).then((res: any) => {
            resolve(res)
            console.log(`res`, res)
        })
    })
}

// 编辑
export async function updateOneGoods(params: IGoodsProps) {
    return new Promise((resolve, reject) => {
        axios
            .post(`${Config.API_BASE_URL}/goods/updateOneGoods`, { ...params })
            .then((res: any) => {
                resolve(res)
                console.log(`res`, res)
            })
    })
}

// 删除
export async function deleteOneGoods(params: string) {
    return new Promise((resolve, reject) => {
        axios
            .post(`${Config.API_BASE_URL}/goods/deleteOneGoods`, { key: params })
            .then((res: any) => {
                resolve(res)
                console.log(`res`, res)
            })
    })
}

// 批量删除
export async function deleteGoodsByKeys(params: string[]) {
    return new Promise((resolve, reject) => {
        axios
            .post(`${Config.API_BASE_URL}/goods/deleteGoodsByKeys`, { keys: params })
            .then((res: any) => {
                resolve(res)
                console.log(`res`, res)
            })
    })
}

// 搜索
export async function getGoodsByName(param: string) {
    return new Promise((resolve, reject) => {
        axios
            .post(`${Config.API_BASE_URL}/goods/getGoodsByName`, { name: param })
            .then((res: any) => {
                resolve(res.data)
                console.log(`res`, res.data)
            })
    })
}
